package com.remiss.seckilldemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.remiss.seckilldemo.pojo.Order;
import com.remiss.seckilldemo.pojo.SeckillOrder;
import com.remiss.seckilldemo.pojo.User;
import com.remiss.seckilldemo.service.IGoodsService;
import com.remiss.seckilldemo.service.IOrderService;
import com.remiss.seckilldemo.service.ISeckillOrderService;
import com.remiss.seckilldemo.vo.GoodsVO;
import com.remiss.seckilldemo.vo.ResposeBean;
import com.remiss.seckilldemo.vo.ResposeBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.List;

@Controller
@RequestMapping("/seckill")
@Slf4j
public class SeckillController {
    @Autowired
    private IGoodsService iGoodsService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostConstruct
    public void init(){
        List<GoodsVO> list = iGoodsService.findGoodsVO();
        for (GoodsVO goodsVO : list){
            redisTemplate.opsForValue().set("goods:" +goodsVO.getId(),goodsVO.getStockCount());
        }
    }

    @RequestMapping(value = "/doSeckill",method = RequestMethod.POST)
    @ResponseBody
    public ResposeBean doSeckill(Model model, User user , Long goodsId){
        if(user == null){
            return ResposeBean.error(ResposeBeanEnum.SESSION_ERROR);
        }
//        model.addAttribute("user",user);
        if (((Integer) redisTemplate.opsForValue().get("goods:" + goodsId)) <= 0){
            return ResposeBean.error(ResposeBeanEnum.EMPTY_STOCK_ERROR);
        }
        GoodsVO goodsVO = iGoodsService.findGoodsVOByGoodsId(goodsId);
        if(goodsVO.getStockCount()<1){
//            model.addAttribute("errormsg", ResposeBeanEnum.EMPTY_STOCK_ERROR.getMessage());
            return ResposeBean.error(ResposeBeanEnum.EMPTY_STOCK_ERROR);
        }
//        判断是否重复抢购
//        SeckillOrder seckillOrderServiceOne = seckillOrderService.getOne(new QueryWrapper<SeckillOrder>()
//                .eq("user_id", user.getId()).eq("goods_id", goodsId));
        SeckillOrder seckillOrder = (SeckillOrder) redisTemplate.opsForValue().get("order:" + user.getId() + ":" + goodsId);

        if(seckillOrder != null){
//            model.addAttribute("errormsg",ResposeBeanEnum.REPEATE_ERROR.getMessage());
            return ResposeBean.error(ResposeBeanEnum.REPEATE_ERROR);
        }
        Order order = orderService.seckill(user,goodsVO);
//        model.addAttribute("order",order);
//        model.addAttribute("goods",goodsVO);
        return ResposeBean.success(order);
    }
}
